package com.tianci.appstore.ui.list.cgylist;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;

import com.tianci.appstore.R;
import com.tianci.appstore.ui.data.SkyCategoryData;
import com.tianci.appstore.ui.widget.SkyCategoryBar;
import com.tianci.appstore.ui.widget.SkyCategoryCursor;
import com.tianci.appstore.ui.widget.TipToast;

public class SkyCategoryClickBar extends SkyCategoryBar {
    private SkyCategoryCursor mClickedCursor;

    public SkyCategoryClickBar(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public void setCategoryDatas(List<SkyCategoryData> categories) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mClickedCursor = new SkyCategoryCursor(mContext);
        mClickedCursor.setLayoutParams(params);
        this.addView(mClickedCursor);

        if (categories != null && categories.size() > 0) {
            super.setCategoryDatas(categories);

            mClickedCursor.setData(mCategoryName.getData());
            mClickedCursor.setSelected(true);
        } else {
            TipToast.getInstance(mContext).showTipToast(mContext.getString(R.string.data_error));
        }
    }

    public SkyCategoryData getCurrentClickedData() {
        if (mCategoryDatas == null)
            return null;
        return mCategoryDatas.get(mClickedPos);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            if (mCategoryCursor != null) {
                mCategoryCursor.setFocusPos(mCurrentPos, false);
                mCategoryCursor.setVisibility(View.VISIBLE);
            }

        } else {
            if (mCategoryCursor != null) {
                mCurrentPos = mClickedPos;
                mCategoryCursor.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    protected void clickCategory() {
        // TODO Auto-generated method stub
        super.clickCategory();
        mClickedCursor.setVisibility(View.INVISIBLE);
        mClickedCursor.setFocusPos(mClickedPos, false);
        mClickedCursor.setVisibility(View.VISIBLE);
    }

    public void setClickedPos(int clickedPos) {
        this.mClickedPos = clickedPos;
        clickCategory();
    }

    @Override
    public void setClickPos(int pos) {
        setClickedPos(pos);
        super.setClickPos(pos);
    }

    public void hideFocusCursor() {
        this.mCategoryCursor.setVisibility(View.INVISIBLE);
    }
}
